/*
 * except.h
 *
 *  Created on: Jan 23, 2025
 *      Author: hudong
 */

#ifndef EXCEPT_H_
#define EXCEPT_H_

#include <setjmp.h>
#include <stdbool.h>

#pragma pack(4)

typedef struct {
  int type;
  char *reason;
} Exception;

enum EXCEPTYPE {
  OutOfMemory,
  NullPointer,
  OutOfBounds,
  IllegalArgument,
  IOException
};

struct __IExcept {
  jmp_buf *(*env)();
  void (*status)(int status);
  bool (*try)();
  bool (*catch)(int type);
  void (*finally)();
  void (*end)();
  Exception *(*exception)();
  void (*throw)(int type, const char *reason);
  void (*error)(const char *format, ...);
  void (*checkAlloc)(void *p, const char *reason);
};
#pragma pack()

extern struct __IExcept Try;

#define TryBegin Try.status(sigsetjmp(*Try.env(), 1))
#define TryEnd Try.end()

#endif /* EXCEPT_H_ */
